﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace httpExplorer.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class InputViewModel : ViewModelBase
    {

        private string _url = Properties.Settings.Default.Url;
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                _url = value;
                Properties.Settings.Default.Url = value;
                RaisePropertyChanged("Url");
            }
        }


        RelayCommand _goCommand;
        public ICommand GoCommand
        {
            get
            {
                if (_goCommand == null)
                {
                    _goCommand = new RelayCommand(
                        GoGet,
                        () => true);
                }
                return _goCommand;
            }
        }


        private void GoGet()
        {
            try
            {
                if (!string.IsNullOrEmpty( Url))
                {
                    if (ViewModelLocator.NetworkSettingsViewModelStatic.IsProxySystem)
                    {
                        ViewModelLocator.ModelLocator.InitiateHttpRequestUsingSystemProxy(Url);                        
                    }
                    else if (ViewModelLocator.NetworkSettingsViewModelStatic.IsProxyCustom)
                    {
                        string proxyUri = string.Format("http://{0}:{1}", ViewModelLocator.NetworkSettingsViewModelStatic.ProxyAddress, ViewModelLocator.NetworkSettingsViewModelStatic.ProxyPort);

                        if (ViewModelLocator.NetworkSettingsViewModelStatic.UseProxyAuthentication)
                        {
                            if (ViewModelLocator.NetworkSettingsViewModelStatic.IsProxyAuthenticationBasic)
                            {
                                string username = ViewModelLocator.NetworkSettingsViewModelStatic.ProxyUsername;
                                string password = ViewModelLocator.NetworkSettingsViewModelStatic.ProxyPassword;
                                string domain = ViewModelLocator.NetworkSettingsViewModelStatic.ProxyDomain;

                                if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(domain))
                                {
                                    ViewModelLocator.ModelLocator.InitiateHttpRequestUsingProxy(Url, proxyUri, username, password, domain);
                                }
                            }
                            else if (ViewModelLocator.NetworkSettingsViewModelStatic.IsProxyAuthenticationLogin)
                            {
                                ViewModelLocator.ModelLocator.InitiateHttpRequestUsingProxy(Url, proxyUri, true);
                            }
                        }
                        else
                        {
                            ViewModelLocator.ModelLocator.InitiateHttpRequestUsingProxy(Url, proxyUri);
                        }
                    }
                    else
                    {
                        ViewModelLocator.ModelLocator.InitiateHttpRequest(Url);                        
                    }
                }
            }
            catch ( System.Net.WebException ex )
            {
                Messenger.Default.Send(new Message.ResponseMessage { Response = (System.Net.HttpWebResponse)ex.Response });
            }
            catch
            {
                Messenger.Default.Send(new Message.ResponseMessage { Response = null });
            }
        }



        /// <summary>
        /// Initializes a new instance of the InputViewModel class.
        /// </summary>
        public InputViewModel()
        {
            //if (IsInDesignMode)
            //{
            //    // Code runs in Blend --> create design time data.
            //}
            //else
            //{
            //    // Code runs "for real"
            //}
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}